/*
CVARS:

[prefix]_auto_buy_cost_type - speciifes if cost should be changed if user is buying items with quick command
0 - nothing
1 - if user is buying automaticly he will pay additional money
realCost = cost + auto_buy_cost_val
2 - if user is buying automaticly he will pay additional % of real cost 
realCost = cost + (cost * auto_buy_cost_val)

[prefix]_auto_buy_cost_val - specifiest additional cost if user is buying items with quick command
if(_auto_buy_cost_type == 1) it is integer
if(_auto_buy_cost_type == 2) it is float

[prefix]_buy_time_type - specifiets rule of checking time when player can buy items
0 - buying is allowed at any time
1 - buying is allowed for mp_buytime minutes
2 - buying is allowed for [prefix]_buy_time_val seconds

[prefix]_buy_time_val - how many seconds for buying items

[prefix]_buy_place_type - specifies where player can buy items
0 - eevrywhere on map
1 - only in buy_zone
2 - only near place he spawns

[prefix]_buy_place_distance - in what distance from spawn point player can buy items (uesd only if _buy_place_type is 2)

*/

/* 	Default value of cvars
*/
#define CVAR_AUTO_BUY_COST_TYPE_DEF "2"
#define CVAR_AUTO_BUY_COST_VAL_DEF "0.1"
#define CVAR_BUY_TIME_TYPE_DEF "2"
#define CVAR_BUY_TIME_VAL_DEF "60"
#define CVAR_BUY_PLACE_TYPE_DEF "1"
#define CVAR_BUY_PLACE_DISTANCE_DEF "100"

/*	Maximum noumber of dealers
If you have only 5 dealers change it to 5
*/
#define MAX_DEALERS 10

/*	Maximum number of players on server +1
if server has 25 slots you can change it to 26
*/
#define MAX_PLAYERS 33

/*	This is prefix added to cvars and natives
For example:
if prefix is "dd" than auto_buy_cost_val cvar will be dd_auto_buy_cost_val
It should be unique if you have multiple dealers
*/
new const prefix[] = "wd";

/*	This is preifx displayed in chat messages
*/
new const PREFIX_DISP[] = "[Weapon Dealer]";

/*	These are items dealer can have
*/
enum _: ITEMS {
	ITEM_VESTHELM,
	WEAPONS_P228,
	ITEM_PRIM_AMMO,
	WEAPONS_SCOUT,
	WEAPONS_HEGRENADE,
	WEAPONS_XM1014,	//5
	ITEM_SEC_AMMO,
	WEAPONS_MAC10,
	WEAPONS_AUG,
	WEAPONS_SMOKEGRENADE,
	WEAPONS_ELITE,		//10
	WEAPONS_FIVESEVEN,
	WEAPONS_UMP45,
	WEAPONS_SG550,
	WEAPONS_GALIL,
	WEAPONS_FAMAS,		//15
	WEAPONS_USP,
	WEAPONS_GLOCK18,
	WEAPONS_AWP,
	WEAPONS_MP5NAVY,
	WEAPONS_M249,		//20
	WEAPONS_M3,
	WEAPONS_M4A1,
	WEAPONS_TMP,
	WEAPONS_G3SG1,
	WEAPONS_FLASHBANG,	//25
	WEAPONS_DEAGLE,
	WEAPONS_SG552,
	WEAPONS_AK47,
	ITEM_DEF,
	WEAPONS_P90		//30
	
}


/*	These are items language specific names
Real names will be taken from dictionary file
*/
new const ITEM_NAMES[ITEMS][] = {
	"ITEM_VESTHELM",
	"WEAPONS_P228",
	"ITEM_PRIM_AMMO",
	"WEAPONS_SCOUT",
	"WEAPONS_HEGRENADE",
	"WEAPONS_XM1014",	//5
	"ITEM_SEC_AMMO",
	"WEAPONS_MAC10",
	"WEAPONS_AUG",
	"WEAPONS_SMOKEGRENADE",
	"WEAPONS_ELITE",		//10
	"WEAPONS_FIVESEVEN",
	"WEAPONS_UMP45",
	"WEAPONS_SG550",
	"WEAPONS_GALIL",
	"WEAPONS_FAMAS",		//15
	"WEAPONS_USP",
	"WEAPONS_GLOCK18",
	"WEAPONS_AWP",
	"WEAPONS_MP5NAVY",
	"WEAPONS_M249",		//20
	"WEAPONS_M3",
	"WEAPONS_M4A1",
	"WEAPONS_TMP",
	"WEAPONS_G3SG1",
	"WEAPONS_FLASHBANG",	//25
	"WEAPONS_DEAGLE",
	"WEAPONS_SG552",
	"WEAPONS_AK47",
	"ITEM_DEF",
	"WEAPONS_P90"	
}

enum _:ML_STRINGS_ENUM {
	mlDealersMenuTitle,
	mlNoAccessToDealer,
	mlCount,
	mlCost,
	mlItemMenuTitle,
	mlItemBought,
	mlNotEnoughMoney,
	mlItemsSold,
	mlCanNotBuy
}

new const ML_STRINGS[ML_STRINGS_ENUM][]={
	"DEALERS_MENU_TITLE" , 	//mlDealersMenuTitle
	"NO_ACCESS_TO_DEALED", 	//mlNoAccessToDealer
	"COUNT", 		//mlCount
	"COST", 			//mlCost
	"ITEMS_MENU_TITLE",	//mlItemMenuTitle
	"ITEM_BOUGHT",		//mlItemBought
	"NOT_ENOUGH_MONEY",	//mlNotEnoughMoney
	"ITEMS_SOLD",		//mlItemsSold
	"CAN_NOT_BUY"		//mlCanNotBuy
}


#define PLUGIN "Dealer"
#define VERSION "1.0"
#define AUTHOR "Zabijaka"


#include "the_dealers\td_def.inc"
#include "the_dealers\td_main.inl"
#include "the_dealers\td_register.inl"
#include "the_dealers\td_menus.inl"
#include "the_dealers\td_tools.inl"


/*	Comment line below if you do not need natives for controling dealers and items from other plugins
*/
#include "the_dealers\td_natives.inl"



#define PRIMARY_WEAPONS_BITSUM ((1<<CSW_SCOUT)|(1<<CSW_XM1014)|(1<<CSW_MAC10)|(1<<CSW_AUG)|(1<<CSW_UMP45)|(1<<CSW_SG550)|(1<<CSW_GALIL)|(1<<CSW_FAMAS)|(1<<CSW_AWP)|(1<<CSW_MP5NAVY)|(1<<CSW_M249)|(1<<CSW_M3)|(1<<CSW_M4A1)|(1<<CSW_TMP)|(1<<CSW_G3SG1)|(1<<CSW_SG552)|(1<<CSW_AK47)|(1<<CSW_P90))
stock bool:is_primary_weapon(id){
	return bool:(PRIMARY_WEAPONS_BITSUM & id);
}

#define SEC_WEAPONS_BITSUM ((1<<CSW_P228)|(1<<CSW_ELITE)|(1<<CSW_FIVESEVEN)|(1<<CSW_DEAGLE)|(1<<CSW_USP)|(1<<CSW_GLOCK18))
stock bool:is_secondary_weapon(id) {
	return bool:(SEC_WEAPONS_BITSUM & id);
}
#include <fun>
new const WEAPON_BP_MAX[31]={
	/* weapon_p228 */ 52,
	/* weapon_shield */ 0,
	/* weapon_scout */ 90,
	/* weapon_hegrenade */ 1,
	/* weapon_xm1014 */ 32,
	/* weapon_c4 */ 1,
	/* weapon_mac10 */ 100,
	/* weapon_aug */ 90,
	/* weapon_smokegrenade */ 1,
	/* weapon_elite */ 120,
	/* weapon_fiveseven */ 100,
	/* weapon_ump45 */ 100,
	/* weapon_sg550 */ 90,
	/* weapon_galil */ 90,
	/* weapon_famas */ 90,
	/* weapon_usp */ 100,
	/* weapon_glock18 */ 120,
	/* weapon_awp */ 30,
	/* weapon_mp5navy */ 120,
	/* weapon_m249 */ 200,
	/* weapon_m3 */ 32,
	/* weapon_m4a1 */ 90,
	/* weapon_tmp */ 120,
	/* weapon_g3sg1 */ 90,
	/* weapon_flashbang */ 2,
	/* weapon_deagle */ 35,
	/* weapon_sg552 */ 90,
	/* weapon_ak47 */ 90,
	/* weapon_knife */ 0,
	/* weapon_p90 */ 100
};
new const WEAPON_CLIP[31] = {
	/* weapon_p228 */ 13,
	/* weapon_shield */ 0,
	/* weapon_scout */ 10,
	/* weapon_hegrenade */ 0,
	/* weapon_xm1014 */ 7,
	/* weapon_c4 */ 0,
	/* weapon_mac10 */ 30,
	/* weapon_aug */ 30,
	/* weapon_smokegrenade */ 0,
	/* weapon_elite */ 30,
	/* weapon_fiveseven */ 20,
	/* weapon_ump45 */ 25,
	/* weapon_sg550 */ 30,
	/* weapon_galil */ 35,
	/* weapon_famas */ 25,
	/* weapon_usp */ 12,
	/* weapon_glock18 */ 20,
	/* weapon_awp */ 10,
	/* weapon_mp5navy */ 30,
	/* weapon_m249 */ 100,
	/* weapon_m3 */ 8,
	/* weapon_m4a1 */ 30,
	/* weapon_tmp */ 30,
	/* weapon_g3sg1 */ 20,
	/* weapon_flashbang */ 0,
	/* weapon_deagle */ 7,
	/* weapon_sg552 */ 30,
	/* weapon_ak47 */ 30,
	/* weapon_knife */ 0,
	/* weapon_p90 */ 50
};

new AMMO_COST[31] = {
	/* weapon_p228 */ 50,
	/* weapon_shield */ 0,
	/* weapon_scout */ 80,
	/* weapon_hegrenade */ 0,
	/* weapon_xm1014 */ 65,
	/* weapon_c4 */ 0,
	/* weapon_aug */ 60,
	/* weapon_smokegrenade */ 0,
	/* weapon_elite */ 20,
	/* weapon_fiveseven */ 50,
	/* weapon_ump45 */ 25,
	/* weapon_sg550 */ 60,
	/* weapon_galil */ 60,
	/* weapon_famas */ 60,
	/* weapon_usp */ 25,
	/* weapon_glock18 */ 20,
	/* weapon_awp */ 125,
	/* weapon_mp5navy */ 20,
	/* weapon_m249 */ 60,
	/* weapon_m3 */ 65,
	/* weapon_m4a1 */ 60,
	/* weapon_tmp */ 20,
	/* weapon_g3sg1 */ 80,
	/* weapon_flashbang */ 0,
	/* weapon_deagle */ 40,
	/* weapon_sg552 */ 60,
	/* weapon_ak47 */ 80,
	/* weapon_knife */ 0
};
new WeaponNames[31][] = {
	"", //Blank
	"weapon_p228",
	"", //Nothing
	"weapon_scout",
	"weapon_hegrenade",
	"weapon_xm1014",
	"", //C4
	"weapon_mac10",
	"weapon_aug",
	"weapon_smokegrenade",
	"weapon_elite",
	"weapon_fiveseven",
	"weapon_ump45",
	"weapon_sg550",
	"weapon_galil",
	"weapon_famas",
	"weapon_usp",
	"weapon_glock18",
	"weapon_awp",
	"weapon_mp5navy",
	"weapon_m249",
	"weapon_m3",
	"weapon_m4a1",
	"weapon_tmp",
	"weapon_g3sg1",
	"weapon_flashbang",
	"weapon_deagle",
	"weapon_sg552",
	"weapon_ak47",
	"", //Knife
	"weapon_p90"
};



public plugin_init() {
	register_plugin(PLUGIN, VERSION, AUTHOR);
	
	//Register your dictionary with item names and ML_STRINGS
	// ...
	
	//Comment line below if you have all ML_STRINGS in your own dictionary
	register_dictionary("the_dealers.txt");
	
	
	register_def_cvars();
	
	// Add your code here
	// ...
}

public plugin_natives(){
	#if defined _td_natives_included
	register_def_natives();
	#endif
}

public client_connect(id){
	client_connect_main(id);
}


/*	Modify this method if you want to disallow buyin some items in some condition
@params:
id - 	players id
item - 	item
returns:
true if you want to allow buying item, false othervise
*/
stock bool:td_can_give_item(id, item) {
	if(!is_user_alive(id)) {
		client_print(id, print_chat, "%s %L", PREFIX_DISP, id, "NEED_TO_BE_ALIVE");
		return false;
	}
	switch(item) {
		case ITEM_DEF: {
			if(cs_get_user_team(id) != CS_TEAM_CT) {
				client_print(id, print_chat, "%s %L", PREFIX_DISP, id, "DEF_FOR_CT");
				return false;
			}
			if(cs_get_user_defuse(id)) {
				client_print(id, print_chat, "%s %L %L", PREFIX_DISP, id, "ALREADY_HAVE", id, ITEM_NAMES[item]);
				return false;
			}
		}
		case ITEM_VESTHELM: {
			new CsArmorType:aType;
			new armor = cs_get_user_armor(id, aType);
			if(armor >= 100) {
				return false;
			}
		}
		case ITEM_PRIM_AMMO: return user_has_prim(id);
			case ITEM_SEC_AMMO: return user_has_sec(id);
			case WEAPONS_FLASHBANG: {
			if(user_has_weapon(id, item) && cs_get_user_bpammo(id, item) >=2) {
				return false;
			}
		}
		default: {
			if(user_has_weapon(id, item)) {
				return false;
			}
		}
	}
	return true;	
}
stock bool:user_has_prim(id){
	new weapons[32];
	new num;
	get_user_weapons(id, weapons, num);
	for(new i=0;i<num;i++) {
		if(is_primary_weapon(weapons[i])) {
			return true;
		}
	}
	return false;
}
stock bool:user_has_sec(id){
	new weapons[32];
	new num;
	get_user_weapons(id, weapons, num);
	for(new i=0;i<num;i++) {
		if(is_secondary_weapon(weapons[i])) {
			return true;
		}
	}
	return false;
}

/*	This method handeles buying items
For example if you have weapon dealer in this method you should give weapon
*/
stock td_item_bought(id, item){
	switch(item){
		case ITEM_DEF: give_item(id, "item_thighpack");
			case ITEM_VESTHELM: cs_set_user_armor(id, 100, CS_ARMOR_VESTHELM);
			case ITEM_PRIM_AMMO: give_ammo(id, true);
			case ITEM_SEC_AMMO: give_ammo(id, false);
			default: {
			give_item(id, WeaponNames[item]);
			new params[1];
			params[0] = item;
			set_task(0.1, "task_give_ammo", id + item * 100, params, 1);
		}
	}
}
public task_give_ammo(params[], taskId){
	new item = params[0];
	new id = taskId - item*100;
	if(is_user_connected(id) && is_user_alive(id)){
		give_weapon_ammo(id, item);
	}
}
stock give_ammo(id, bool:prim){
	new weapons[32];
	new num;
	new weapon;
	get_user_weapons(id, weapons, num);
	new cost;
	new money = cs_get_user_money(id);

	new bp;
	new clip;
	new maxBp;
	for(new i=0;i<num;i++){
		weapon = weapons[i];
		if((prim && is_primary_weapon(weapon)) || (!prim && is_secondary_weapon(weapon))){
			cost = AMMO_COST[weapon];
			bp = cs_get_user_bpammo(id, weapon);
			clip = WEAPON_CLIP[weapon];
			maxBp = WEAPON_BP_MAX[weapon];
			while( (money> cost) && ((bp + clip) <= maxBp)){
				money -= cost;
				bp+= clip;
			}
			cs_set_user_bpammo(id, weapon, bp);
		}
	}
	cs_set_user_money(id, money);
}

stock give_weapon_ammo(id, weapon){
	new cost;
	new money = cs_get_user_money(id);
	new bp;
	new clip;
	new maxBp;
	
	if(is_primary_weapon(weapon)||  is_secondary_weapon(weapon)){
		cost = AMMO_COST[weapon];
		bp = cs_get_user_bpammo(id, weapon);
		clip = WEAPON_CLIP[weapon];
		maxBp = WEAPON_BP_MAX[weapon];
		while((money> cost) && ((bp + clip) <= maxBp)){
			money -= cost;
			bp+= clip;
		}
		cs_set_user_bpammo(id, weapon, bp);
	}
	cs_set_user_money(id, money);
}
stock drop_sec_weapons(id){
	client_cmd(id, "drop weapon_p228");
	client_cmd(id, "drop weapon_elite");
	client_cmd(id, "drop weapon_fiveseven");
	client_cmd(id, "drop weapon_deagle");
	client_cmd(id, "drop weapon_usp");
	client_cmd(id, "drop weapon_glock18");
}
stock drop_prim_weapons(id){
	client_cmd(id, "drop weapon_scout");
	client_cmd(id, "drop weapon_xm1014");
	client_cmd(id, "drop weapon_mac10");
	client_cmd(id, "drop weapon_aug");
	client_cmd(id, "drop weapon_ump45");
	client_cmd(id, "drop weapon_sg550");
	client_cmd(id, "drop weapon_galil");
	client_cmd(id, "drop weapon_famas");
	client_cmd(id, "drop weapon_awp");
	client_cmd(id, "drop weapon_mp5navy");
	client_cmd(id, "drop weapon_m249");
	client_cmd(id, "drop weapon_m3");
	client_cmd(id, "drop weapon_m4a1");
	client_cmd(id, "drop weapon_tmp");
	client_cmd(id, "drop weapon_g3sg1");
	client_cmd(id, "drop weapon_sg552");
	client_cmd(id, "drop weapon_ak47");
	client_cmd(id, "drop weapon_p90");
}

